﻿using System.Collections.Generic;
using DocumentFormat.OpenXml.Wordprocessing;
using T2V.SharePoint.Report.DocXGenerator;

namespace Pesico.Forms.DocxGenerator
{
    public class ContractGenerator : BaseDoxGenerator
    {
        protected const string CompanyA = "CompanyA";
        protected const string AddressA = "AddressA";
        protected const string AccountA = "AccountA";
        protected const string PhoneNumberA = "PhoneNumberA";
        protected const string FaxA = "FaxA";
        protected const string RepresentativeA = "RepresentativeA";

        protected const string CompanyB = "CompanyB";
        protected const string AddressB = "AddressB";
        protected const string AccountB = "AccountB";
        protected const string PhoneNumberB = "PhoneNumberB";
        protected const string FaxB = "FaxB";
        protected const string RepresentativeB = "RepresentativeB";
        
        public ContractGenerator(DocumentGenerationInfo generationInfo)
            : base(generationInfo)
        {

        }

        #region Overridden methods

        /// <summary>
        /// Gets the place holder tag to type collection.
        /// </summary>
        /// <returns></returns>
        protected override Dictionary<string, PlaceHolderType> GetPlaceHolderTagToTypeCollection()
        {
            Dictionary<string, PlaceHolderType> placeHolderTagToTypeCollection = base.GetPlaceHolderTagToTypeCollection();

            // Handle recursive placeholders            
            
            // Handle non recursive placeholders
            placeHolderTagToTypeCollection.Add(CompanyA, PlaceHolderType.NonRecursive);
            placeHolderTagToTypeCollection.Add(AddressA, PlaceHolderType.NonRecursive);
            placeHolderTagToTypeCollection.Add(AccountA, PlaceHolderType.NonRecursive);
            placeHolderTagToTypeCollection.Add(PhoneNumberA, PlaceHolderType.NonRecursive);
            placeHolderTagToTypeCollection.Add(FaxA, PlaceHolderType.NonRecursive);
            placeHolderTagToTypeCollection.Add(RepresentativeA, PlaceHolderType.NonRecursive);

            placeHolderTagToTypeCollection.Add(CompanyB, PlaceHolderType.NonRecursive);
            placeHolderTagToTypeCollection.Add(AddressB, PlaceHolderType.NonRecursive);
            placeHolderTagToTypeCollection.Add(AccountB, PlaceHolderType.NonRecursive);
            placeHolderTagToTypeCollection.Add(PhoneNumberB, PlaceHolderType.NonRecursive);
            placeHolderTagToTypeCollection.Add(FaxB, PlaceHolderType.NonRecursive);
            placeHolderTagToTypeCollection.Add(RepresentativeB, PlaceHolderType.NonRecursive);
            
            return placeHolderTagToTypeCollection;
        }

        /// <summary>
        /// Non recursive placeholder found.
        /// </summary>
        /// <param name="placeholderTag">The placeholder tag.</param>
        /// <param name="openXmlElementDataContext">The open XML element data context.</param>
        protected override void NonRecursivePlaceholderFound(string placeholderTag, OpenXmlElementDataContext openXmlElementDataContext)
        {
            if (openXmlElementDataContext == null || openXmlElementDataContext.Element == null || openXmlElementDataContext.DataContext == null)
            {
                return;
            }

            string tagPlaceHolderValue = string.Empty;
            string tagGuidPart = string.Empty;
            GetTagValue(openXmlElementDataContext.Element as SdtElement, out tagPlaceHolderValue, out tagGuidPart);

            string tagValue = string.Empty;
            string content = string.Empty;

            // Reuse base class code and handle only tags unavailable in base class
            bool bubblePlaceHolder = true;

            switch (tagPlaceHolderValue)
            {
                case CompanyA:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).CompanyA;
                    content = tagValue;
                    break;
                case AddressA:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).AddressA;
                    content = tagValue;
                    break;
                case AccountA:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).AccountA;
                    content = tagValue;
                    break;
                case PhoneNumberA:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).PhoneNumberA;
                    content = tagValue;
                    break;
                case FaxA:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).FaxA;
                    content = tagValue;
                    break;
                case RepresentativeA:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).RepresentativeA;
                    content = tagValue;
                    break;

                case CompanyB:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).CompanyB;
                    content = tagValue;
                    break;
                case AddressB:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).AddressB;
                    content = tagValue;
                    break;
                case AccountB:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).AccountB;
                    content = tagValue;
                    break;
                case PhoneNumberB:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).PhoneNumberB;
                    content = tagValue;
                    break;
                case FaxB:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).FaxB;
                    content = tagValue;
                    break;
                case RepresentativeB:
                    bubblePlaceHolder = false;
                    tagValue = ((openXmlElementDataContext.DataContext) as ContractDataContext).RepresentativeB;
                    content = tagValue;
                    break;
            }

            if (bubblePlaceHolder)
            {
                // Use base class code as tags are already defined in base class.
                base.NonRecursivePlaceholderFound(placeholderTag, openXmlElementDataContext);
            }
            else
            {
                // Set the tag for the content control
                if (!string.IsNullOrEmpty(tagValue))
                {
                    this.SetTagValue(openXmlElementDataContext.Element as SdtElement, GetFullTagValue(tagPlaceHolderValue, tagValue));
                }

                // Set the content for the content control
                this.SetContentOfContentControl(openXmlElementDataContext.Element as SdtElement, content);
            }
        }

        #endregion
    }
}
